import { PrismaClient } from '@prisma/client'

const prisma = new PrismaClient()

async function main() {
  try {
    // 创建用户会话表
    await prisma.$executeRaw`
      CREATE TABLE IF NOT EXISTS "user_sessions" (
        "id" TEXT NOT NULL,
        "user_id" TEXT NOT NULL,
        "token" TEXT NOT NULL,
        "ip" TEXT,
        "user_agent" TEXT,
        "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
        "expires_at" TIMESTAMP(3) NOT NULL,
        "is_active" BOOLEAN NOT NULL DEFAULT true,
        CONSTRAINT "user_sessions_pkey" PRIMARY KEY ("id")
      )
    `

    // 创建唯一索引
    await prisma.$executeRaw`
      CREATE UNIQUE INDEX IF NOT EXISTS "user_sessions_token_key" ON "user_sessions"("token")
    `

    // 创建索引
    await prisma.$executeRaw`
      CREATE INDEX IF NOT EXISTS "user_sessions_user_id_idx" ON "user_sessions"("user_id")
    `

    // 创建索引
    await prisma.$executeRaw`
      CREATE INDEX IF NOT EXISTS "user_sessions_token_idx" ON "user_sessions"("token")
    `

    console.log('用户会话表创建成功')
  } catch (error) {
    console.error('创建用户会话表失败:', error)
  } finally {
    await prisma.$disconnect()
  }
}

main()